import moment from 'moment'
import dicService from '../api/dicService'

export default {
  async getMappingData(targetData, kjfwxxjlInfo, vc, extend) {
    const sourceData = vc._.cloneDeep(targetData)
    // eslint-disable-next-line no-extend-native
    String.prototype.replaceAll = function(source, target) {
      let str = this
      while (str.indexOf(source) >= 0) {
        str = str.replace(source, target)
      }
      return str
    }
    // 接件信息
    sourceData.bz_projectcase &&
      sourceData.bz_projectcase.length > 0 &&
      sourceData.bz_projectcase.forEach(item => {
        if (vc) {
          if (vc.projectInfo && vc.projectInfo.activityName === '接件') {
            item.jjdw = vc.userUnit && vc.userUnit.fullName
            item.jjrq = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
          }
        }
      })

    sourceData.bz_xs_xmjbqk &&
      sourceData.bz_xs_xmjbqk.length > 0 &&
      sourceData.bz_xs_xmjbqk.forEach(item => {
        if (item.xmszxzqmc) {
          const value = item.xmszxzqmc.split(';')
          if (value.length > 1) {
            const target = value.join(',')
            item.xmszxzqmc = target
          }
        }
      })

    // 处理字段拼接
    const dicMapping = await dicService.treeDicItemByDicName('上报_字段拼接', '/')
    if (dicMapping && dicMapping.length > 0) {
      dicMapping.forEach(item => {
        const values = item.value.split(',')
        if (sourceData && sourceData[item.name]) {
          const tdyt = {}
          sourceData[item.name].forEach(i => {
            if (values.length > 0 && i[values[1]]) {
              tdyt[i[values[0]]] = Number((i[values[1]] / 100).toFixed(4))
            }
          })
          if (vc._.size(tdyt)) {
            sourceData[item.name] = [{ tdyt: JSON.stringify(tdyt) }]
          }
        }
      })
    }

    let result = {}

    let smData = [] // TODO 空间范围信息记录三个业务只能上传一条记录（后续部里解决后在修改）
    if (kjfwxxjlInfo.bz_td_kcdjjssm && kjfwxxjlInfo.bz_td_kcdjjssm.length > 0) {
      smData = [kjfwxxjlInfo.bz_td_kcdjjssm[0]]
    }
    const resourceData = kjfwxxjlInfo.dr_matterresource || []
    const kcdjqd = kjfwxxjlInfo.bz_td_kcdjqd || []
    const dkData = kjfwxxjlInfo.bz_td_jzdcgb || []
    const jzdData = kjfwxxjlInfo.bz_td_dkzbb || []
    const smForm = []
    smData.length > 0 &&
      smData.forEach(item => {
        const data = resourceData.find(val => val.id === item.f_resourceid)
        if (data) {
          const dkForm = resourceData.find(val => {
            return val.f_parentid === data.f_parentid && val.f_name === '建设用地地块清单'
          })
          smForm.push({
            key: data,
            value: item,
            dkForm: dkForm
          })
        }
      })
    resourceData.length > 0 &&
      resourceData.forEach(item => {
        const data = smForm.find(val => (val.key && val.key.f_parentid) === item.id)
        if (data) {
          data.key = item
        }
      })
    // 资料清单
    smForm.length > 0 &&
      smForm.forEach(item => {
        const data = resourceData.find(val => {
          return (item.key && item.key.f_parentid) === val.f_parentid && val.f_name === '勘测定界资料清单'
        })
        if (data) {
          kcdjqd.length > 0 &&
            kcdjqd.forEach(val => {
              if (val.f_resourceid === data.id && val.kcdjmc === (item.key && item.key.f_name)) {
                val.kc_primary_id = val.id
                item.key = val
                item.key.zbx = item.value && item.value.zbx
                item.key.dw = item.value && item.value.dw
                item.key.jd = item.value && item.value.jd
              }
            })
        }
      })
    const jlInfo = smForm.map(item => item.key)
    const dkInfo = []
    const jzdInfo = []

    smForm.length > 0 &&
      smForm.forEach(item => {
        const dkResourceId = item.dkForm && item.dkForm.id
        dkData.length > 0 &&
          dkData.forEach(val => {
            if (val.f_resourceid === dkResourceId) {
              val.kc_primary_id = item.key && item.key.kc_primary_id
              val.kjwz = 1
              if (val.fwlx === '项目占地范围') {
                val.fwlx = 0
              } else if (val.fwlx === '补划永久基本农田范围') {
                val.fwlx = 3
              }
              if (val.kc_primary_id) {
                dkInfo.push(val)
              }
            }
          })
      })

    dkInfo.length > 0 &&
      dkInfo.forEach(record => {
        jzdData.length > 0 &&
          jzdData.forEach(val => {
            if (val.f_baseid === record.f_baseid && val.f_resourceid === record.f_resourceid) {
              val.kc_primary_id = record.kc_primary_id
              val.dkbh = record.dkbh
              if (val.kc_primary_id) {
                jzdInfo.push(val)
              }
            }
          })
      })

    result = {
      bz_td_kcdjqd: jlInfo,
      bz_td_jzdcgb: dkInfo,
      bz_td_dkzbb: jzdInfo,
      ...sourceData
    }
    return result
  },
  getHandlerApproveParams(reportExtendList, projectId, taskId, businessName) {
    const extendInfo = reportExtendList[0]
    let groupCodes = extendInfo.extendValue
    let dataConfigName = extendInfo.extProperty1
    const extProperty2 = extendInfo.extProperty2
    if (extProperty2 && extProperty2 === 'xkcx') {
      const data = JSON.parse(groupCodes)
      const configName = JSON.parse(dataConfigName)
      groupCodes = data[businessName]
      dataConfigName = configName[businessName]
    }

    return {
      projectId: projectId,
      taskId: taskId,
      groupCodes,
      dataConfigName,
      activityName: extendInfo.activityName
    }
  },
  getExtendParams(projectId, taskId, processState) {
    let filterText
    if (processState === '补正') {
      filterText = '补正申报'
    } else {
      filterText = '项目上报'
    }
    return { projectId, taskId, filterText }
  },
  getReportProcess () {
    return false
  }
}
